<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/19
 * Time: 17:54
 */

namespace Game\Services\Server;


class DispatcherServer
{
    /**
     * 根据操作码分发操作
     * @param $req_ary
     * @return mixed
     */
    public static function dispatcherOpt($req_ary)
    {
        $Base_Model = new Base_Model();

        if(isset($req_ary['operation']))
        {
            switch($req_ary['operation'])
            {
                /*************
                server模块
                 **************/
                case 'ReturnTimerid':		//绑定group id
                    $Server_Timer_Model = new Server_Timer_Model();
                    $result = $Server_Timer_Model->setTimerInfo($req_ary);
                    break;


                case 'StartGamePassive':
                    $Server_Timer_Model = new Server_Timer_Model();
                    $result = $Server_Timer_Model->startGamePassive($req_ary);
                    self::writeLog($req_ary['operation']);
                    break;
                case 'DiscardPassive':
                    $Server_Timer_Model = new Server_Timer_Model();
                    $result = $Server_Timer_Model->discardPassive($req_ary);
                    self::writeLog($req_ary['operation']);
                    break;

                case 'ClearRoomPassive':
                    $Server_Timer_Model = new Server_Timer_Model();
                    $result = $Server_Timer_Model->clearRoomPassive($req_ary);
                    self::writeLog($req_ary['operation']);
                    break;

                default:
                    $result = OPT_CONST::MISSING_OPERATION;	//数据格式不正确，缺少操作码
                    break;
            }

            //关闭数据库
            @$Base_Model->closeMysql();

            return $result;
        }
        else
        {
            return OPT_CONST::MISSING_OPERATION;	//缺少操作码
        }
    }

    /*
        写日志
    */
    public static function writeLog($content)
    {
        $content = date("Y-m-d H:i:s").' '.$content;

        $log_filename = 'operation_'.date("Y-m-d").'.log';

        //$file = __DIR__.'/logs/error.log';
        $file = dirname(dirname(__DIR__)).'/Logs/'.$log_filename;

        file_put_contents($file, $content,FILE_APPEND);

        file_put_contents($file, "\n",FILE_APPEND);
    }
}